package euler.p001_050;

import euler.MainEuler;
import euler.helper.NaturalHelper;

public class Euler034 extends MainEuler {

    /*
        145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

        Find the sum of all numbers which are
        equal to the sum of the factorial of their digits.

        Note: as 1! = 1 and 2! = 2 are not sums they are not included.

     */
    public String resolve() {
        int sumaFactorial = 0;
        int limite = (int)NaturalHelper.factorial(9)*7;

        for (int i = 3; i < limite; i++) {
            int suma = 0;
            int n = i;
            while ((suma <= i) && (n > 0)) {
                suma+=NaturalHelper.factorial(n%10);
                n/=10;
            }

            if (suma == i) {
                sumaFactorial+=i;
            }
        }

        return String.valueOf(sumaFactorial);
        // 40730
    }

}
